package com.example.carrierapp.ui.main.find.detail;

import android.app.Activity;
import android.content.Context;

import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.bean.find.FindGoodsDetailBean;
import com.example.carrierapp.net.ApiMiddleHelper;
import com.example.carrierapp.net.listener.DataCallBackListener;
import com.example.carrierapp.util.LogUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;

public class FindGoodsDetailPresenter extends BasePresenter<FindGoodsDetailContract.View> implements FindGoodsDetailContract.Presenter {
    private Context mContext;
    public FindGoodsDetailPresenter(Activity findGoodsDetailActivity) {
        mContext=findGoodsDetailActivity;
    }

    @Override
    public void start() {

    }

    @Override
    public void getFindGoodsDetail(int id) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("id", id + "");
        Flowable<JsonObject> flowable = apiStrores.getFindGoodsDetaill(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext,flowable, new DataCallBackListener() {

            @Override
            public void onSuccess(String json) {
                LogUtil.d("getFindGoodsDetail-onSuccess-" + json);

                FindGoodsDetailBean bean = new Gson().fromJson(json, new TypeToken<FindGoodsDetailBean>() {
                }.getType());
                if (getView() != null) {
                    getView().getFindGoodsDetailSuccess(bean);
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("getFindGoodsDetail-onFailure-" + msg);
                if (getView() != null) {
                    getView().getFindGoodsDetailFailde(msg);
                }
            }
        });
    }

    @Override
    public void rabOrBidding(int id,String money) {
        Map<String, String> biddingMap = new HashMap<>();

        try {
//            String data = new Gson().toJson(list, new TypeToken<List<MypriceBean>>() {
//            }.getCarTypeName());
//            biddingMap.put("listGrabDtos", data);
            biddingMap.put("id", id + "");
            biddingMap.put("money", money + "");

            Flowable<JsonObject> flowable = apiStrores.rabOrBidding(biddingMap);
            ApiMiddleHelper.getInstance().toNetIo2Main(mContext,flowable, new DataCallBackListener() {

                @Override
                public void onSuccess(String json) {
                    LogUtil.d("rabOrBidding-onSuccess-" + json);
                    if (getView() != null) {
                        getView().rabOrBiddingSuccess(json);
                    }
                }

                @Override
                public void onFailure(String msg) {
                    LogUtil.d("rabOrBidding-onFailure-" + msg);
                    if (getView() != null) {
                        getView().rabOrBiddingFailde(msg);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            if (getView() != null) {
                getView().rabOrBiddingFailde("操作失败，请稍后再试");
            }
        }
    }
}
